package cn.afactory.tygz.ui.sns;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tendcloud.tenddata.TCAgent;

import cn.afactory.tygz.R;
import cn.afactory.tygz.ui.base.BaseFragmentActivity;
import cn.afactory.tygz.ui.base.utils.SPUtils;
import cn.afactory.tygz.ui.sns.fragment.TopicListFragment;


public class TopicListActivity extends BaseFragmentActivity implements View.OnClickListener {
    private LinearLayout ll_attention_topics,ll_create_topics;
    private TextView tv_attention_topics,tv_create_topics;
    private ImageView iv_attention_topics,iv_create_topics;
    private ViewPager mViewPager;

    public static final int USER_LIST_TYPE_ATTETION_TOPICS = 1;
    public static final int USER_LIST_TYPE_CREATE_TOPICS = 0;

    public static final int TYPE_CHOOSE_TOPIC_ID = 1001;

    private int mType = 0;
    @Override
    public void onResume() {
        super.onResume();
        TCAgent.onResume(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mType = getIntent().getIntExtra(SPUtils.EXTRA_SELECT_TYPE,0);

        setContentView(R.layout.ac_topic_list);
        initViews();
        initTitleBar();
    }

    private void initTitleBar(){
        if(mType == TYPE_CHOOSE_TOPIC_ID){
            setTitleText("选择一个话题发表");
        }else {
            setTitleText("我关注/我创建的");
        }
        showBackView();
    }

    /**
     * 跳转到话题列表
     * @param context
     */
    public static void gotoTopicList(Context context){
        Intent intent = new Intent(context,TopicListActivity.class);
        context.startActivity(intent);
    }

    /**
     * 选择一个话题发表
     * @param context
     */
    public static void gotoSelectTopicId(Activity context){
        Intent intent = new Intent(context,TopicListActivity.class);
        intent.putExtra(SPUtils.EXTRA_SELECT_TYPE,TYPE_CHOOSE_TOPIC_ID);
        context.startActivityForResult(intent, PublishSubjectActivity.REQ_TOPIC_ID);
    }

    private void initViews(){
        ll_attention_topics = (LinearLayout)findViewById(R.id.ll_attention_topics);
        ll_create_topics = (LinearLayout)findViewById(R.id.ll_create_topics);

        tv_attention_topics = (TextView)findViewById(R.id.tv_attention_topics);
        tv_create_topics = (TextView)findViewById(R.id.tv_create_topics);

        iv_attention_topics = (ImageView)findViewById(R.id.iv_attention_topics);
        iv_create_topics = (ImageView)findViewById(R.id.iv_create_topics);

        mViewPager = (ViewPager)findViewById(R.id.vp_task_list);

        ll_attention_topics.setOnClickListener(this);
        ll_create_topics.setOnClickListener(this);

        AttentionListViewPagerAdapter adapter = new AttentionListViewPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(adapter);
        mViewPager.setOffscreenPageLimit(2);
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset,
                                       int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int idx) {
                if (idx == USER_LIST_TYPE_ATTETION_TOPICS) {
                    switchTab(USER_LIST_TYPE_ATTETION_TOPICS);
                } else if (idx == USER_LIST_TYPE_CREATE_TOPICS) {
                    switchTab(USER_LIST_TYPE_CREATE_TOPICS);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        switchTab(USER_LIST_TYPE_ATTETION_TOPICS);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ll_attention_topics:
                switchTab(USER_LIST_TYPE_ATTETION_TOPICS);
                break;
            case R.id.ll_create_topics:
                switchTab(USER_LIST_TYPE_CREATE_TOPICS);
                break;
        }
    }

    class AttentionListViewPagerAdapter extends FragmentPagerAdapter {
        public AttentionListViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int idx) {
            Fragment fragment = null;
            switch (idx){
                case USER_LIST_TYPE_ATTETION_TOPICS:
                    fragment = new TopicListFragment(USER_LIST_TYPE_ATTETION_TOPICS,mType);
                    break;
                case USER_LIST_TYPE_CREATE_TOPICS:
                    fragment = new TopicListFragment(USER_LIST_TYPE_CREATE_TOPICS,mType);
            }
            return fragment;
        }

        @Override
        public int getCount() {
            return 2;
        }
    }
    //tab之间切换
    private void switchTab(int index) {
        clearTabStatus();
        mViewPager.setCurrentItem(index,true);
        switch (index){
            case USER_LIST_TYPE_ATTETION_TOPICS:
                ll_attention_topics.setSelected(true);
                tv_attention_topics.setSelected(true);
                iv_attention_topics.setVisibility(View.VISIBLE);
                break;
            case USER_LIST_TYPE_CREATE_TOPICS:
                ll_create_topics.setSelected(true);
                tv_create_topics.setSelected(true);
                iv_create_topics.setVisibility(View.VISIBLE);
                break;
        }
    }
    //清除Tab选择状态
    private void clearTabStatus(){
        ll_attention_topics.setSelected(false);
        tv_attention_topics.setSelected(false);
        iv_attention_topics.setVisibility(View.GONE);

        ll_create_topics.setSelected(false);
        tv_attention_topics.setSelected(false);
        iv_create_topics.setVisibility(View.GONE);
    }
}